﻿using System.Web;
using System.Web.Caching;
using System.Web.UI;
using Microsoft.SharePoint;

namespace BoldBrick.SharePoint.CommonControls.Utils
{
	public static class CacheDependencyHelper
	{
		#region AddCacheItemDependency

		/// <summary>
		/// Add <see cref="Page"/> dependency to cache item
		/// </summary>
		/// <param name="page"><see cref="Page"/> object</param>
		/// <param name="context">Current <see cref="HttpContext"/></param>
		public static void AddCacheItemDependency(Page page, HttpContext context)
		{
			string key = GetDependencyKey(page);
			AddCacheItemDependency(key, context);
		}

		/// <summary>
		/// Add <see cref="SPListItem"/> dependency to cache item
		/// </summary>
		/// <param name="listItem"><see cref="SPListItem"/> object</param>
		/// <param name="context">Current <see cref="HttpContext"/></param>
		public static void AddCacheItemDependency(SPListItem listItem, HttpContext context)
		{
			string key = GetDependencyKey(listItem);
			AddCacheItemDependency(key, context);
		}

		/// <summary>
		/// Add <see cref="SPWeb"/> dependency to cache item
		/// </summary>
		/// <param name="web"><see cref="SPWeb"/> object</param>
		/// <param name="context">Current <see cref="HttpContext"/></param>
		public static void AddCacheItemDependency(SPWeb web, HttpContext context)
		{
			string key = GetDependencyKey(web);
			AddCacheItemDependency(key, context);
		}
		
		/// <summary>
		/// Add dependency to cache item
		/// </summary>
		/// <param name="key">Key of object in cache</param>
		/// <param name="context">Current <see cref="HttpContext"/></param>
		public static void AddCacheItemDependency(string key, HttpContext context)
		{
			bool isCached = CacheHelper.IsCached(key);
			if (!isCached)
				CacheHelper.Set(key, true, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable);

			context.Response.AddCacheItemDependency(key);
		}

		#endregion


		#region GetDependencyKey

		/// <summary>
		/// Creates dependency key for <see cref="Page"/> object
		/// </summary>
		/// <param name="page"><see cref="Page"/> object</param>
		/// <returns>Returns dependency key</returns>
		public static string GetDependencyKey(Page page)
		{
			string key = string.Format("BoldBrick|{0}", page.Request.Url.AbsolutePath);
			return key;
		}

		/// <summary>
		/// Creates dependency key for <see cref="SPListItem"/> object
		/// </summary>
		/// <param name="listItem"><see cref="SPListItem"/> object</param>
		/// <returns>Returns dependency key</returns>
		public static string GetDependencyKey(SPListItem listItem)
		{
			string key = string.Format("BoldBrick|{0}", listItem.Url);
			return key;
		}

		/// <summary>
		/// Creates dependency key for <see cref="SPWeb"/> object
		/// </summary>
		/// <param name="web"><see cref="SPWeb"/> object</param>
		/// <returns>Returns dependency key</returns>
		public static string GetDependencyKey(SPWeb web)
		{
			string key = string.Format("BoldBrick|{0}", web.ID);
			return key;
		}

		#endregion

	}
}
